package com.ymatou.iapi.spu.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productspu.parameter.GetSpuPropertyListBean;
import com.ymatou.iapi.productspu.parameter.PropertyOut;
import com.ymatou.iapi.productspu.parameter.SPUDTO;
import com.ymatou.iapi.productspu.service.GetSpuPropertyListCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.SpuPropertyWapper;
import com.ymttest.utils.Logger2Debug;
/**
*获取SPU属性列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSpuPropertyList{
	private static GetSpuPropertyListBean getspupropertylistBean;
	private static GetSpuPropertyListCall getspupropertylistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取SPU属性列表");
	}

	@Before
	public void caseUp() {
		getspupropertylistBean=new GetSpuPropertyListBean();
		getspupropertylistCall=new GetSpuPropertyListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getspupropertylistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer spuId, List<PropertyOut> pout) throws InterruptedException {
		SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
		List<Map> spupinfo = SpuPropertyWapper.selectSpuPropertyBySpuId(spuId);
		Map<String,Map> mspu=new HashMap<>();
		spupinfo.forEach(sp->{
			mspu.put(sp.get("PropertyId").toString()+sp.get("InputValues").toString(),sp);
		});
//		PropertyId+InputValues
//		Logger.debug(spupinfo);
		int i=0;
		pout.forEach(po->{
			Logger.debug(FormatUtil.beanToGSONString(po));
			String key ="";
			if(po.getInputValues()==null||po.getInputValues().isEmpty()){
				 key = po.getPropertyId().toString();
			}else{
				key = po.getPropertyId()+(po.getInputValues()==null?"":po.getInputValues().get(0));
			}
			Map spu = mspu.get(key);
			Logger.debug(spu);
			Logger.verifyEquals(spu.get("Name"), po.getPropertyName(), "验证PropertyName");
			if(spu.get("Values")!=null){
			//	Logger.verifyEquals(spu.get("Values").toString().split(",").length, po.getValues().size(), "验证Values");
			}
			Logger.verifyEquals(spu.get("MultiSelect"), po.getIsMultiple(), "验证MultiSelect");
			Logger.verifyEquals(spu.get("Styles"), po.getPropertyValueType(), "验证Styles");
		});
		
		
	}
//	private void verifyDBDebug(Integer spuId, SPUDTO spu) throws InterruptedException {
//		SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
//		Map spuinfo = SpuPropertyWapper.selectSpuProductInfoBySpuid(spuId);
//		Logger2Debug.verifyEquals(spuinfo.get("BarCode")==null?null:spuinfo.get("BarCode"),spu.getBarCode(),"验证BarCode");
//		Logger2Debug.verifyEquals(spuinfo.get("BrandEnName")==null?null:spuinfo.get("BrandEnName"),spu.getBrandEnName(),"验证BrandEnName");
//		Logger2Debug.verifyEquals(spuinfo.get("BrandId")==null?0:spuinfo.get("BrandId"),spu.getBrandId(),"验证BrandId");
//		Logger2Debug.verifyEquals(spuinfo.get("BrandName")==null?null:spuinfo.get("BrandName"),spu.getBrandName(),"验证BrandName");
//		Logger2Debug.verifyEquals(spuinfo.get("Height")==null?0:spuinfo.get("Height"),spu.getHeight(),"验证Height");
//		Logger2Debug.verifyEquals(spuinfo.get("IsPspSpu")==null?null:spuinfo.get("IsPspSpu"),spu.getIsPspSpu(),"验证IsPspSpu");
//		Logger2Debug.verifyEquals(spuinfo.get("IsRecmd")==null?false:spuinfo.get("IsRecmd"),spu.getIsRecmd(),"验证IsRecmd");
//		Logger2Debug.verifyEquals(spuinfo.get("Length")==null?0:spuinfo.get("Length"),spu.getLength(),"验证Length");
//		Logger2Debug.verifyEquals(spuinfo.get("MarketingTime")==null?null:spuinfo.get("MarketingTime"),spu.getMarketingTime(),"验证MarketingTime");
//		Logger2Debug.verifyEquals(spuinfo.get("MasterCategoryId")==null?null:spuinfo.get("MasterCategoryId"),spu.getMasterCategoryId(),"验证MasterCategoryId");
//		Logger2Debug.verifyEquals(spuinfo.get("MasterCategoryName")==null?null:spuinfo.get("MasterCategoryName"),spu.getMasterCategoryName(),"验证MasterCategoryName");
//		Logger2Debug.verifyEquals(spuinfo.get("CountryName")==null?null:spuinfo.get("CountryName"),spu.getOrigin(),"验证Origin");
//		Logger2Debug.verifyEquals(spuinfo.get("PriceEnd")==null?null:new BigDecimal(spuinfo.get("PriceEnd").toString()).setScale(2),spu.getPriceEnd().setScale(2),"验证PriceEnd");
//		Logger2Debug.verifyEquals(spuinfo.get("PriceStart")==null?null:new BigDecimal(spuinfo.get("PriceStart").toString()).setScale(2),spu.getPriceStart().setScale(2),"验证PriceStart");
//		Logger2Debug.verifyEquals(spuinfo.get("ProductDescription")==null?null:spuinfo.get("ProductDescription"),spu.getProductDescription(),"验证ProductDescription");
//		Logger2Debug.verifyEquals(spuinfo.get("ProductName")==null?null:spuinfo.get("ProductName"),spu.getProductName(),"验证ProductName");
////		Logger2Debug.verifyEquals(spuinfo.get("ProductPictures")==null?null:spuinfo.get("ProductPictures"),spu.getProductPictures(),"验证ProductPictures");
//		Logger2Debug.verifyEquals(spuinfo.get("ProductSummary")==null?null:spuinfo.get("ProductSummary"),spu.getProductSummary(),"验证ProductSummary");
//		Logger2Debug.verifyEquals(spuinfo.get("SecondCategoryId")==null?null:spuinfo.get("SecondCategoryId"),spu.getSecondCategoryId(),"验证SecondCategoryId");
//		Logger2Debug.verifyEquals(spuinfo.get("SecondCategoryName")==null?null:spuinfo.get("SecondCategoryName"),spu.getSecondCategoryName(),"验证SecondCategoryName");
//		Logger2Debug.verifyEquals(spuinfo.get("SpuId")==null?null:spuinfo.get("SpuId"),spu.getSPUId(),"验证SPUId");
//		Logger2Debug.verifyEquals(spuinfo.get("ThirdCategoryId")==null?null:spuinfo.get("ThirdCategoryId"),spu.getThirdCategoryId(),"验证ThirdCategoryId");
//		Logger2Debug.verifyEquals(spuinfo.get("ThirdCategoryName")==null?null:spuinfo.get("ThirdCategoryName"),spu.getThirdCategoryName(),"验证ThirdCategoryName");
//		Logger2Debug.verifyEquals(spuinfo.get("Unit")==null?null:spuinfo.get("Unit"),spu.getUnit(),"验证Unit");
//		Logger2Debug.verifyEquals(spuinfo.get("Weight")==null?0:spuinfo.get("Weight"),spu.getWeight(),"验证Weight");
//		Logger2Debug.verifyEquals(spuinfo.get("Width")==null?0:spuinfo.get("Width"),spu.getWidth(),"验证Width");
//		
//		List<String> pp = spu.getProductPictures();
//		Collections.sort(pp);
//		List<String> pp2=(List<String>)( spuinfo.get("ProductPics")==null?new ArrayList<>():Arrays.asList(spuinfo.get("ProductPics").toString().split(",")));
//		Collections.sort(pp2);
//		Logger2Debug.verifyEquals(pp2.toString(),pp.toString(),"验证ProductPictures");
//		
//		List<String> pdp = spu.getProductDescriptionPics();
//		Collections.sort(pp);
//		
//		List<String> pdp2=(List<String>)( spuinfo.get("DescPics")==null?new ArrayList<>():Arrays.asList(spuinfo.get("DescPics").toString().split(",")));
//		Collections.sort(pp2);
//		Logger2Debug.verifyEquals(pdp2.toString(),pdp.toString(),"验证ProductDescriptionPics");
//		
//		List<Map> pinfo = SpuPropertyWapper.selectSpuPropertyInfosBySpuid(spuId);
//		Map<String,Map> mpinfo=new HashMap<>();
//		pinfo.forEach(m->mpinfo.put(m.get("Values").toString(),m));
//		Logger2Debug.verifyEquals(pinfo.size(), spu.getPropertyPairs().size(), "验证PropertyPairs的size");
//		spu.getPropertyPairs().forEach(pps->{
//			Map mpp = mpinfo.get(pps.getPropertyValue());
//			Logger2Debug.verifyEquals(mpp.get("PropertyName")==null?null:mpp.get("PropertyName").toString(), pps.getPropertyName(), "验证PropertyName");
//			Logger2Debug.verifyEquals(mpp.get("PropertyId")==null?null:mpp.get("PropertyId"), pps.getPropertyId(), "验证PropertyId");
//			Logger2Debug.verifyEquals(mpp.get("Status")==null?null:mpp.get("Status").toString(), pps.getPropertyStatus().toString(), "验证PropertyStatus");
//		});
//	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSpuPropertyList_001() {
		Logger.start(true,"获取SPU属性列表成功");
		try {
			Integer spuid=262936;
			getspupropertylistBean.setSPUId(spuid);
			getspupropertylistCall.setData(getspupropertylistBean);
			getspupropertylistCall.callService();			
			List<PropertyOut> pout = getspupropertylistCall.getPropertyList();
			
			List<Map> spucinfo =new SpuPropertyWapper().selectSpuCategoryBySpuId(spuid);
			Logger.verifyEquals(spucinfo.get(0).get("ProductName"),getspupropertylistCall.getJsonObject().get("Result").getAsJsonObject().get("SpuName").getAsString(),"验证SpuName");
			Logger.verifyEquals("200", getspupropertylistCall.getString("Status"), "验证Status");
			verifyDB(spuid, pout);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetSpuPropertyList_002() {
		Logger.start(true,"不输入ProductId失败");
		try {
			getspupropertylistCall.setData(getspupropertylistBean);
			getspupropertylistCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetSpuPropertyList_003() {
		Logger.start(true,"核对全部数据,获取SPU属性列表成功");
		try {
			//存在问题 属性值id重复导致对比获取数据错误
			new SpuPropertyWapper().selectAllSpuProduct().forEach(spup->{
				getspupropertylistBean.setSPUId(spup.getSpuId());
				getspupropertylistCall.setData(getspupropertylistBean);
				try {
					Logger2Debug.debug(spup.getSpuId());
					getspupropertylistCall.callService();
					Logger2Debug.verifyEquals("200", getspupropertylistCall.getString("Status"), "验证Status");
//					verifyDBDebug(spup.getSpuId(),getspupropertylistCall.getSPU());
				} catch (Exception e) {
					e.printStackTrace();
				}	
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}